package com.jin.lazytool.utils;

import com.jin.lazytool.JinTaskController;
import com.jin.lazytool.entity.IEvent;
import com.jin.lazytool.rewritte.FxJinTask;
import com.jin.lazytool.task.JinTask;
import com.jin.lazytool.task.JinTaskGroup;
import javafx.scene.control.TableView;

public class CurrentStateUtil {
    // 正在收集的任务组
    private static JinTaskGroup JIN_TASK_GROUP_COLLECTING;
    // 正在收集任务组 上一个事件监听事件（执行时间隔时间）
    private static long JIN_TASK_GROUP_COLLECTING_PRE_TIME;
    // 当前选中的任务
    private static JinTask SELECT_TASK;
    // 运行中的任务
    private static FxJinTask<?> RUNNING_JIN_SERVICE;

    public static void startCollect(JinTaskGroup taskGroup,TableView<IEvent> tableView,long time){
        JIN_TASK_GROUP_COLLECTING = taskGroup;
        JIN_TASK_GROUP_COLLECTING_PRE_TIME = time;
    }
    public static void stopCollect(){
        JIN_TASK_GROUP_COLLECTING.getEvents().remove(JIN_TASK_GROUP_COLLECTING.getEvents().size() - 1);
        JIN_TASK_GROUP_COLLECTING.getEvents().remove(JIN_TASK_GROUP_COLLECTING.getEvents().size() - 1);
        JIN_TASK_GROUP_COLLECTING.getEvents().remove(JIN_TASK_GROUP_COLLECTING.getEvents().size() - 1);
        if(!SELECT_TASK.getGroups().contains(JIN_TASK_GROUP_COLLECTING)){
            SELECT_TASK.addTaskGroup(JIN_TASK_GROUP_COLLECTING);
            JinTaskController.controller.initTablePane(SELECT_TASK);
        }else{
            JIN_TASK_GROUP_COLLECTING.getTableView().refresh();
        }
    }

    public static void addEventOfGroup(IEvent event){
        long currentTimeMillis = System.currentTimeMillis();
        long sub = currentTimeMillis - JIN_TASK_GROUP_COLLECTING_PRE_TIME;
        JIN_TASK_GROUP_COLLECTING_PRE_TIME = currentTimeMillis;
        event.setTime(sub);
        JIN_TASK_GROUP_COLLECTING.addEvent(event);
    }

    public static JinTask getSelectTask(){
        return SELECT_TASK;
    }
    public static void setSelectTask(JinTask task){
        SELECT_TASK = task;
        JinTaskController.controller.initTablePane(SELECT_TASK);
    }

    public static void setRunningJinTask(FxJinTask<?> task){
        RUNNING_JIN_SERVICE = task;
    }
    public static FxJinTask<?> getRunningJinTask(){
        return RUNNING_JIN_SERVICE;
    }
}
